package com.moli.iov.admin.model.vo.res;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.moli.iov.admin.utils.Contant;
import com.moli.iov.admin.utils.DictFactory;
import com.moli.iov.base.model.dto.res.VehicleBaseTboxResDto;
import com.moli.iov.down.model.po.VehicleParam;
import com.moli.iov.protocol.model.dto.realtime.RealTimeInfoDto;
import com.moli.iov.protocol.model.realtime.*;
import com.moli.iov.protocol.util.RealTimeInfoUtil;
import com.moli.iov.real.enums.OnlineTag;
import com.moli.iov.util.JsonUtils;
import java.util.Date;
import java.util.List;
import java.util.Map;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;


@Slf4j
@Data
public class VehicleDetailVo {

    public VehicleDetailVo() {
    }

    private String fenceNames;
    //
    private String vehicleTypeName;

    private String deviceModel;

    private String lastOdoReportTime;

    //vin
    private String vin;
    //车牌号
    private String license;
    //累积里程
    private double odometer;
    //GPS里程
    private int extendGpsOde;
    //注册时间
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    private Date registerDate;
    //经度
    private double longitude;
    //纬度
    private double latitude;


    //***************************************tbox_info
//供应商名称   supplierName
//tbox硬件版本型号
    private String tboxVersion;

    //软件版本
    private String softwareVersion;

    //硬件版本
    private String hardwareVersion;

    //终端id
    private String deviceId;

    //sim卡号
    private String simNo;

    private String iccid;

    //最后上报时间
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    private Date reportDate;

    //在线状态
    private String onlineStatus;

    //******************************MonitorInfo
//充电状态
    private String chargeStatus;
    //运行模式
    private String runMode;
    //车速
    private double speed;
    //总电压(V)
    private double totalVoltage;
    //总电流(A)
    private double totalCurrent;
    //SOC
    private byte soc;
    //DC-DC状态
    private String dcStatus;
    //档位
    private String gear;
    //电源档位
    private String powerGear;
    //绝缘电阻(Insulation Resistance) kΩ
    private int insResistance;
    /**
     * 加速踏板行程值 有效值范围：0～100(表示0%～100%)； 最小计量单元：1%； "0xFE"表示异常，"0xFF"表示无效。
     */
    private byte speedUpValue;

    /**
     * 制动踏板行程值
     * <p>
     * 有效值范围：0～100(表示0%～100%)； 最小计量单元：1%； “0”表示制动关状态；在无具体形成值情况下，用“0x65”即“101”表示制动有效状态，
     * "0xFE"表示异常，"0xFF"表示无效。
     */
    private byte brakeTravelValue;

    //***********************************************************VehicleStateInfo
    //车门状态
    private String doorStatus;
    //车窗状态
    private String windowStatus;
    //车灯
    private String lampStatus;
    //租赁模式
    private String leasingStatus;
    //蓝牙状态
    private String bluetoothStatus;
    //物理钥匙
    private String physicalKeyStatus;
    //动力状态
    private String dynamicStatus;
    //USB上网
    private String usbInternetStatus;
    //蓄电池电压
    private double extendBatteryVoltage;
    //手刹状态
    private String handBrakeStatus;
    //中控状态
    private String extendControlLockState;
    //充电枪状态
    private String chargingGunState;
    //GPS速度
    private int gpsSpeed;
    //GPS方向角
    private int gpsDirection;
    //终端定位星数
    private int extendDeviceLocationCount;
    //通信信号强度
    private int extendSignalIntensity;

    private DriveMotorReq driveMotor;

    private LimitValueReq limitValue;

    private BmsTemperatureReq bmsTemperature;

    private BmsVoltageReq bmsVoltage;

    private String locationStatus;

    private VehicleAuthVo auth;

    private VehicleAlarmStatusVo alarmStatus;

    private Double realOdo;

//    //城市
//    private String regionName;
//    //档位
//    private String gear;
//    //定位状态
//    private String locationStatus;
//    //驱动电机序号
//    private short motorSerialNum;
//    //控制器温度
//    private short motorControlTemperature;
//    //转速
//    private int motorRotateSpeed;
//    //输入电压
//    private double motorControlVoltage;
//
//    //驱动电机状态
//    private String motorStatus;
//
//    //驱动电机温度
//    private short motorTemperature;
//
//    //转矩
//    private double motorTorque;
//
//    //母线电流
//    private double motorControlCurrent;

    public static void main(String[] args) {
        String str = "{\"bmsTemperature\":{\"listBmsTemps\":[{\"bytesLen\":3,\"probeTemperature\":[],\"probeTemperatureBytes\":\"AAAAAAAAAAAAAAAAAAA=\",\"probeTemperatureToString\":\"\",\"storageSubSystemCode\":1,\"storageSubSystemProbeSum\":14}],\"storageSubSystemSum\":1},\"bmsVoltage\":{\"listBmsVoltages\":[{\"batteryCellFinally\":84,\"batteryCellStartSernum\":1,\"batteryCellSum\":84,\"batteryCellVoltage\":[4.08,4.08,4.08,4.08,4.08,4.08,4.08,4.08,4.08,4.08,4.08,4.08,4.06,4.06,4.08,4.06,4.06,4.06,4.06,4.06,4.06,4.06,4.06,4.06,4.08,4.08,4.08,4.08,4.08,4.08,4.06,4.08,4.08,4.08,4.08,4.08,4.08,4.08,4.08,4.08,4.08,4.08,4.08,4.08,4.08,4.08,4.08,4.08,4.08,4.08,4.08,4.08,4.08,4.08,4.08,4.08,4.08,4.08,4.08,4.08,4.08,4.08,4.08,4.08,4.08,4.08,4.08,4.08,4.08,4.08,4.08,4.08,4.08,4.08,4.08,4.08,4.08,4.08,4.06,4.08,4.08,4.06,4.08,4.08],\"batteryCellVoltage1\":\"D/AP8A/wD/AP8A/wD/AP8A/wD/AP8A/wD9sP2w/wD9sP2w/bD9sP2w/bD9sP2w/bD/AP8A/wD/AP8A/wD9sP8A/wD/AP8A/wD/AP8A/wD/AP8A/wD/AP8A/wD/AP8A/wD/AP8A/wD/AP8A/wD/AP8A/wD/AP8A/wD/AP8A/wD/AP8A/wD/AP8A/wD/AP8A/wD/AP8A/wD/AP8A/wD9sP8A/wD9sP8A/w\",\"batteryCellVoltageToString\":\"4.08,4.08,4.08,4.08,4.08,4.08,4.08,4.08,4.08,4.08,4.08,4.08,4.06,4.06,4.08,4.06,4.06,4.06,4.06,4.06,4.06,4.06,4.06,4.06,4.08,4.08,4.08,4.08,4.08,4.08,4.06,4.08,4.08,4.08,4.08,4.08,4.08,4.08,4.08,4.08,4.08,4.08,4.08,4.08,4.08,4.08,4.08,4.08,4.08,4.08,4.08,4.08,4.08,4.08,4.08,4.08,4.08,4.08,4.08,4.08,4.08,4.08,4.08,4.08,4.08,4.08,4.08,4.08,4.08,4.08,4.08,4.08,4.08,4.08,4.08,4.08,4.08,4.08,4.06,4.08,4.08,4.06,4.08,4.08\",\"bytesLen\":10,\"storageCurrent\":0.0,\"storageSubSystemCode\":1,\"storageVoltage\":342.4}],\"storageSubSystemSum\":1},\"collectDate\":\"2019-06-13 17:40:10\",\"driveMotor\":{\"driveMotorSum\":1,\"listDriveMotors\":[{\"motorControlCurrent\":0.0,\"motorControlTemperature\":44,\"motorControlVoltage\":344.0,\"motorRotateSpeed\":2526,\"motorSerialNum\":1,\"motorState\":\"POWER_CONSUMPTION\",\"motorTemperature\":35,\"motorTorque\":29.9}]},\"extendVehicleState\":{\"bluetoothState\":\"OPEN\",\"doorState5\":\"CLOSE\",\"doorState6\":\"CLOSE\",\"doorState7\":\"CLOSE\",\"dynamicState\":\"OPEN\",\"extendBatteryVoltage\":13.9,\"extendChargingGunState\":\"UNCONNECTED\",\"extendDeviceLocationCount\":14,\"extendDoorState\":0,\"extendGpsDirection\":72,\"extendGpsOde\":8739,\"extendGpsSpeed\":36,\"extendHandBrakeState\":\"OFF\",\"extendLeasing\":\"YES\",\"extendLeasingModel\":29,\"extendLength\":20,\"extendLightState\":0,\"extendLocationState\":\"VALID\",\"extendPowerGear\":\"ON\",\"extendSignalIntensity\":0,\"extendWindowState\":0,\"frontFogLamp\":\"CLOSE\",\"highBeam\":\"CLOSE\",\"lamp5\":\"CLOSE\",\"lamp6\":\"CLOSE\",\"lamp7\":\"CLOSE\",\"leftBackDoor\":\"CLOSE\",\"leftBackWindow\":\"CLOSE\",\"leftFrontDoor\":\"CLOSE\",\"leftFrontWindow\":\"CLOSE\",\"lowBeam\":\"CLOSE\",\"physicalKeyState\":\"OPEN\",\"readingLamp\":\"CLOSE\",\"rearDoor\":\"CLOSE\",\"rearFogLamp\":\"CLOSE\",\"rightBackDoor\":\"CLOSE\",\"rightBackWindow\":\"CLOSE\",\"rightFrontDoor\":\"CLOSE\",\"rightFrontWindow\":\"CLOSE\",\"usbInternetState\":\"CLOSE\",\"window4\":\"CLOSE\",\"window5\":\"CLOSE\",\"window6\":\"CLOSE\",\"window7\":\"CLOSE\"},\"limitValue\":{\"bytesLen\":14,\"cellHighestVoltage\":4.08,\"cellLowestVoltage\":4.06,\"highestCellSingleCode\":1,\"highestCellSysCode\":1,\"highestTemperature\":26,\"highestTemperatureProbeNum\":1,\"highestTemperatureSysCode\":1,\"lowestCellSingleCode\":13,\"lowestCellSysCode\":1,\"lowestTemperature\":25,\"lowestTemperatureProbeNum\":4,\"lowestTemperatureSysCode\":1},\"reportDate\":\"2019-06-13 14:26:12\",\"vehicleAll\":{\"brakeTravelValue\":0,\"chargeState\":\"NO_CHARGING\",\"dcStateTag\":\"WORKING\",\"gear\":{\"existDriveForces\":\"NO\",\"exstBreaking\":\"NO\",\"gear\":\"DRIVE\",\"k\":\"无驱动力-无制动力-自动D挡\",\"v\":14},\"insResistance\":10000,\"odometer\":8628.0,\"runMode\":\"EV\",\"soc\":88,\"speed\":33.1,\"speedUpValue\":0,\"totalCurrent\":0.0,\"totalVoltage\":342.4,\"vehicleState\":\"STARTING\"},\"vehicleGps\":{\"latitude\":31.305826,\"locationState\":{\"isValid\":\"YES\",\"k\":\"定位-北纬-东经\",\"lat\":\"NORTH\",\"lon\":\"EAST\",\"v\":0},\"longitude\":117.905635},\"vin\":\"123\"}";
        RealTimeInfoDto dto = JsonUtils.parse(str, RealTimeInfoDto.class);
//    VehicleDetailVo res = new VehicleDetailVo(null, dto, true, null, null);
//        res.setOnlineStatus();
//        res.setdeviceModel
//                vin
//        license
//                registerDate
//        vehicleTypeName
//                deviceId
//        simNo
//                iccid
//        organizationInfo
//                selledStatus
//        softwareVersion
//                hardwareVersion
//        longitude = gps.getLongitude();
//        //纬度
//        latitude
//    System.out.println(JsonUtils.toJson(res));
    }

    public VehicleDetailVo(VehicleBaseTboxResDto vehicleBaseTboxDto, RealTimeInfoReq realTimeDto,
                           Boolean onlineState, Map<String, Map<String, String>> dictMap,
                           VehicleParam param, RealTimeInfo info) {

        if (info != null) {
            setLastOdoReportTime(RealTimeInfoUtil.getLastOdoReportTime(info));
            setExtendGpsOde(RealTimeInfoUtil.getGpsOde(info));

        }

        if (vehicleBaseTboxDto != null) {

            if (dictMap.get("TBOXN") != null
                    && !StringUtils.isEmpty(vehicleBaseTboxDto.getSupplierName())
                    && !StringUtils.isEmpty(vehicleBaseTboxDto.getDeviceModel())) {
                String supplierName = dictMap.get("TBOXN").get(vehicleBaseTboxDto.getSupplierName());
                String deviceModel = vehicleBaseTboxDto.getDeviceModel();
                if (!StringUtils.isEmpty(supplierName)
                        && !StringUtils.isEmpty(deviceModel)) {
                    this.deviceModel = supplierName + "-" + deviceModel;
                }
            }

            DictFactory dictFactory = new DictFactory(dictMap);
            this.vin = vehicleBaseTboxDto.getVin();
            this.license = vehicleBaseTboxDto.getLicense();
            //累积里程
            //GPS里程
            this.registerDate = vehicleBaseTboxDto.getRegisterTime();
            this.vehicleTypeName = "--";
            if (dictMap.get("CLPP") != null
                    && dictMap.get("VTYPE") != null
                    && dictMap.get("CHEX") != null
                    && !StringUtils.isEmpty(vehicleBaseTboxDto.getBrand())
                    && !StringUtils.isEmpty(vehicleBaseTboxDto.getVehicleType())
                    && !StringUtils.isEmpty(vehicleBaseTboxDto.getVehicleSeries())) {
                String brand = dictMap.get("CLPP").get(vehicleBaseTboxDto.getBrand());
                String vehicleType = dictMap.get("VTYPE").get(vehicleBaseTboxDto.getVehicleType());
                String vehicleSeries = dictMap.get("CHEX").get(vehicleBaseTboxDto.getVehicleSeries());
                if (!StringUtils.isEmpty(brand)
                        && !StringUtils.isEmpty(vehicleType)
                        && !StringUtils.isEmpty(vehicleSeries)) {
                    this.vehicleTypeName = brand + vehicleSeries + vehicleType;
                }
            }

            deviceId = vehicleBaseTboxDto.getDeviceId();
            simNo = vehicleBaseTboxDto.getSimNo();
            iccid = vehicleBaseTboxDto.getIccid();
            //在线状态
            //最后上线时间
//            registerDate = DateUtil.format(DateUtil.YEAR_SECOND, vehicleBaseTboxDto.getRegisterTime());
        }
        if (param != null) {
            //软件版本
            softwareVersion = param.getDeviceSoftVersion();
            //硬件版本
            hardwareVersion = param.getDeviceHardVersion();
        }
        if (realTimeDto != null) {
            reportDate = realTimeDto.getCollectDate().getDate();
            VehicleAllReq allDto = realTimeDto.getVehicleAllReq();
            ExtendVehicleState vehicleStateDto = realTimeDto.getExtendReq().extendVehicleState;
            VehicleGpsReq gps = realTimeDto.getVehicleGpsReq();
            driveMotor = realTimeDto.getDriveMotorReq();

            limitValue = realTimeDto.getLimitValueReq();
            if (limitValue != null) {
                if (limitValue.getCellHighestVoltage() == -98) {
                    limitValue.setCellHighestVoltage(0);
                }
                if (limitValue.getHighestCellSysCode() == -98) {
                    limitValue.setHighestCellSysCode((short) 0);
                }

                if (limitValue.getHighestCellSingleCode() == -98) {
                    limitValue.setHighestCellSingleCode((short) 0);
                }

                if (limitValue.getLowestCellSysCode() == -98) {
                    limitValue.setLowestCellSysCode((short) 0);
                }

                if (limitValue.getLowestCellSingleCode() == -98) {
                    limitValue.setLowestCellSingleCode((short) 0);
                }

                if (limitValue.getCellLowestVoltage() == -98) {
                    limitValue.setCellLowestVoltage(0);
                }

                if (limitValue.getHighestTemperatureSysCode() == -98) {
                    limitValue.setHighestTemperatureSysCode((short) 0);
                }

                if (limitValue.getHighestTemperatureProbeNum() == -98) {
                    limitValue.setHighestTemperatureProbeNum((short) 0);
                }
                if (limitValue.getHighestTemperature() == -98) {
                    limitValue.setHighestTemperature((short) 0);
                }

                if (limitValue.getLowestTemperatureSysCode() == -98) {
                    limitValue.setLowestTemperatureSysCode((short) 0);
                }
                if (limitValue.getLowestTemperatureProbeNum() == -98) {
                    limitValue.setLowestTemperatureProbeNum((short) 0);
                }
                if (limitValue.getLowestTemperature() == -98) {
                    limitValue.setLowestTemperature((short) 0);
                }
            }
            bmsTemperature = realTimeDto.getBmsTemperatureReq();
            bmsVoltage = realTimeDto.getBmsVoltageReq();
            if (bmsVoltage != null) {
                List<BmsVoltageInfo> bmsVoltages = bmsVoltage.getListBmsVoltages();
                if (bmsVoltages != null && bmsVoltages.size() > 0) {
                    for (BmsVoltageInfo voltage : bmsVoltages) {
                        if (voltage != null) {
                            if (voltage.getStorageCurrent() == -98) {
                                voltage.setStorageCurrent(0);
                            }
                            if (voltage.getStorageVoltage() == -98) {
                                voltage.setStorageVoltage(0);
                            }
                        }
                    }
                }
            }

            if (allDto != null) {
                //充电状态
                this.chargeStatus = RealTimeInfoUtil.getChargeStatus(allDto);
                //运行模式
                this.runMode = RealTimeInfoUtil.getRunModel(allDto);
                this.speed = RealTimeInfoUtil.getSpeed(allDto);
                this.totalVoltage = RealTimeInfoUtil.getTotalVoltage(allDto);
                this.totalCurrent = RealTimeInfoUtil.getTotalCurrent(allDto);
                //最后上线时间
                this.reportDate = realTimeDto.getCollectDate().getDate();
                this.soc = RealTimeInfoUtil.getSoc(allDto);
                this.dcStatus = RealTimeInfoUtil.getDcStatus(allDto);
                //档位
                this.gear = RealTimeInfoUtil.getGear(allDto);
                //累积里程
                this.odometer = RealTimeInfoUtil.getOdometer(allDto);
                //绝缘电阻
                this.insResistance = RealTimeInfoUtil.getInsResistance(allDto);
                //加速踏板行程
                speedUpValue = RealTimeInfoUtil.getSpeedUpValue(allDto);
                //制动踏板位置
                brakeTravelValue = RealTimeInfoUtil.getBrakeTravelValue(allDto);
            }

            if (gps != null) {
                //经度
                longitude = gps.getLongitude();
                //纬度
                latitude = gps.getLatitude();
            }

            if (vehicleStateDto != null) {
                //车身数据
                //车门状态
                this.doorStatus = RealTimeInfoUtil.getDoorStatus(vehicleStateDto);
                //车窗状态
                this.windowStatus = RealTimeInfoUtil.getWindowStatus(vehicleStateDto);
                //车灯状态
                this.lampStatus = RealTimeInfoUtil.getLampStatus(vehicleStateDto);
                //租赁状态
                this.leasingStatus = RealTimeInfoUtil.getLeasingStatus(vehicleStateDto);
                //物理钥匙
                this.physicalKeyStatus = RealTimeInfoUtil.getPhysicalKeyStatus(vehicleStateDto);
                //动力装填
                this.dynamicStatus = RealTimeInfoUtil.getDynamicStatus(vehicleStateDto);
                //USB
                usbInternetStatus = RealTimeInfoUtil.getUsbInternetStatus(vehicleStateDto);
                //蓝牙
                bluetoothStatus = RealTimeInfoUtil.getBluetoothStatus(vehicleStateDto);
                //电源档位
                this.powerGear = RealTimeInfoUtil.getPowerGear(vehicleStateDto);
                //蓄电池电压
                extendBatteryVoltage = RealTimeInfoUtil.getExtendBatteryVoltage(vehicleStateDto);
                //手刹
                handBrakeStatus = RealTimeInfoUtil.getHandBrakeStatus(vehicleStateDto);
                //中控锁
                extendControlLockState = RealTimeInfoUtil.getExtendControlLockState(vehicleStateDto);
                //充电枪
                chargingGunState = RealTimeInfoUtil.getChargingGunState(vehicleStateDto);
//
//        extendGpsOde = vehicleStateDto.getExtendGpsOde();
                //GPS速度
                gpsSpeed = RealTimeInfoUtil.getExtendGpsSpeed(vehicleStateDto);
                //GPS方向角
                gpsDirection = RealTimeInfoUtil.getExtendGpsDirection(vehicleStateDto);
                //终端定位星数
                extendDeviceLocationCount = RealTimeInfoUtil.getExtendDeviceLocationCount(vehicleStateDto);
                //信号强度
                extendSignalIntensity = RealTimeInfoUtil.getExtendSignalIntensity(vehicleStateDto);
                //定位状态
                locationStatus = RealTimeInfoUtil.getLocationStatusK(realTimeDto.getVehicleGpsReq());
            }

//            motorSerialNum = RealTimeInfoUtil.getMotorSerialNum(driveMotor);
//            motorControlTemperature = RealTimeInfoUtil.getMotorControlTemperature(driveMotor);
//            motorRotateSpeed = RealTimeInfoUtil.getMotorRotateSpeed(driveMotor);
//            motorControlVoltage = RealTimeInfoUtil.getMotorControlVoltage(driveMotor);
//            motorStatus = RealTimeInfoUtil.getMotorStatus(driveMotor);
//            motorTemperature = RealTimeInfoUtil.getMotorTemperature(driveMotor);
//            motorTorque = RealTimeInfoUtil.getMotorTorque(driveMotor);
//            motorControlCurrent = RealTimeInfoUtil.getMotorControlCurrent(driveMotor);
        }
//在线状态
        if (onlineState != null) {
            onlineStatus = OnlineTag.getKey(onlineState);
        } else {
            onlineStatus = Contant.NULL;
        }

//        if (cityMap != null) {
//            regionName = cityMap.get(vehicleBaseTboxDto.getCityId());
//            if (regionName == null) {
//                regionName = Contant.NULL;
//            }
//        }
    }
}
